package start.spring.basic.interceptor;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 
 * ****************************************************************************
 * 全局spring mvc配置
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2023年2月8日
 ******************************************************************************
 */
@Configuration
public class GlobalMvcConfigurer implements WebMvcConfigurer {
	
	@Resource
	TraceIdInterceptor traceIdInterceptor;
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(traceIdInterceptor).addPathPatterns("/**");
	}

	FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
       FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

       FastJsonConfig fastJsonConfig=new FastJsonConfig();
       fastJsonConfig.setSerializerFeatures(
               SerializerFeature.QuoteFieldNames,
               SerializerFeature.WriteMapNullValue,
               SerializerFeature.DisableCircularReferenceDetect,
               SerializerFeature.WriteDateUseDateFormat,
               SerializerFeature.WriteNullStringAsEmpty);
       
       SerializeConfig serializeConfig = new SerializeConfig();
       serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
//       serializeConfig.put(Long.class, ToStringSerializer.instance);
//       serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
       fastJsonConfig.setSerializeConfig(serializeConfig);
       fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

       List<MediaType> mediaTypeList=new ArrayList<>();
       mediaTypeList.add(MediaType.APPLICATION_JSON);
       fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypeList);
       fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
       return fastJsonHttpMessageConverter;
    }

	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		
		converters.clear();
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        converters.add(converter);        
        converters.add(fastJsonHttpMessageConverter());
	}

}
